﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessSell
    {
        #region Create
        public static void createSell(Sell s)
        {
            try
            {
                dataSell.createSell(s);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating sell");
            }
        }

        public static void createSell(User u, Region r, Material m, float price, String currency)
        {
            try
            {
                Sell s = new Sell();
                s.UserID = u.UserID;
                s.RegionID = r.RegionID;
                s.MaterialID = m.MaterialID;
                s.Price = price;
                s.Currency = currency;

                dataSell.createSell(s);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating sell");
            }
        }
        #endregion

        #region Get
        public static Sell getSell(Guid guid)
        {
            try
            {
                return dataSell.getSell(guid);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting sell");
            }
        }

        public static List<Sell> getSellByUser(User u)
        {
            try
            {
                Expression<Func<Sell, bool>> whereClause = (Sell s) => (s.UserID == u.UserID);

                return dataSell.getSells(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting sell by user");
            }
        }

        public static List<Sell> getSellByRegion(Region r)
        {
            try
            {
                Expression<Func<Sell, bool>> whereClause = (Sell s) => (s.RegionID == r.RegionID);

                return dataSell.getSells(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting sell by region");
            }
        }

        public static List<Sell> getSellByMaterial(Material m)
        {
            try
            {
                Expression<Func<Sell, bool>> whereClause = (Sell s) => (s.MaterialID == m.MaterialID);

                return dataSell.getSells(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting sell by material");
            }
        }

        public static List<Sell> getSellByMaterialAndRegion(Material m, Region r)
        {
            try
            {
                Expression<Func<Sell, bool>> whereClause = (Sell s) => (s.MaterialID == m.MaterialID) && (s.RegionID == r.RegionID);

                return dataSell.getSells(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting sell by material and region");
            }
        }
        #endregion

        #region Set
        public static Boolean changeSell(Sell s)
        {
            try
            {
                return dataSell.changeSell(s);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error changing sell");
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteSell(Sell s)
        {
            try
            {
                return dataSell.deleteSell(s);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error deleting sell");
            }
        }
        #endregion
    }
}
